function [V, h, k, Ak] = JacobiRotationAlgorithm( A, eps )
N = size(A, 1);
t = NonDiagonalElementsSum(A);
Ap = A;
Vk = eye(N);
k = 0;
while (t >= eps)
    [max, max_i, max_j] = FindMaxInSymmetricMatrix(Ap);
    if Ap(max_i, max_i) == Ap(max_j, max_j)
        cos_fi = cos(pi / 4);
        sin_fi = sin(pi / 4);
    else
        x = 2 * max;
        y = Ap(max_i, max_i) - Ap(max_j, max_j);
        cos_fi = sqrt(1/2 * (1 + abs(y) / sqrt(x^2 + y^2)));
        sin_fi = sign(x * y) * abs(x) / (2 * cos_fi * sqrt(x^2 + y^2));
    end;
    v = eye(N);
    v(max_i, max_i) = cos_fi;
    v(max_j, max_j) = cos_fi;
    v(max_i, max_j) = -sin_fi;
    v(max_j, max_i) = sin_fi;
    Vk = Vk * v;
    Ap = v' * Ap * v;
    t = t - 2 * max^2;
    k = k + 1;
end;
V = Vk;
h = zeros(N, 1);
for i = 1 : N
    h(i,1) = Ap(i,i);
end
Ak = Ap;


for i = 1 : N
    T = V(:, i:i);
    U =  A * T - h(i) * T;
    dlmwrite('lab3_report.txt', U, '-append', 'precision', '%.4f', 'delimiter', '\t', 'roffset', 1);
end;


